package com.tienon.message.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;



import com.tienon.message.entity.Raw_msg_batch;
import com.tienon.message.thread.task.RawMsgTask;


/**
 * 原始表批量线程组
 * @author xiuye
 *
 */
public class LoadRawMsgTasks {

	private static ExecutorService pool = null;
	
	private static CompletionService<Boolean> csPool = null;
	
	static{
		pool = Executors.newFixedThreadPool(15);
		csPool = new ExecutorCompletionService<Boolean>(pool);
	}
	
	/**
	 * 加载单条数据并返回future结果
	 * @param data
	 * @return
	 */
	public static Future<Boolean> loadRawMsgTask(Raw_msg_batch data){
		
		return csPool.submit(new RawMsgTask(data));
		
	}
	
	/**
	 * 加载多条数据并返回一批future结果
	 * @param datas
	 * @return
	 */
	public static List<Future<Boolean>> loadRawMsgTasks(List<Raw_msg_batch> datas){
		
		List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();
		
		for(Raw_msg_batch data : datas){
			
			futures.add(csPool.submit(new RawMsgTask(data)));
			
		}
		
		return futures;
		
		
	}
	
	/**
	 * 关闭线程组
	 */
	public static void closeTasks(){
		
		pool.shutdown();
		pool = null;
		csPool = null;
		
	}
	
	/**
	 * 载入新的线程组
	 */
	public static void openTasks(){
		pool = Executors.newFixedThreadPool(15);
		csPool = new ExecutorCompletionService<Boolean>(pool);
	}
	
}
